'''
Mainly for sliver price checking

#白银T+D 以下均为Silver ETF的价格：
1： 收盘开盘的差95%在0.93，70%在0.47内。
2： 20日差95%在2.38，70%在1.4内。
3： 涨1天以后继续涨的概率是60%，隔天还涨的话就变成30%
4： 跌的概率高一些，第二天涨概率60%, 继续跌的概率40%，第三天是20%

'''

#coding=utf-8
import time  
from selenium import webdriver  
import re
import tkinter as tk

driver = webdriver.Chrome(r'C:\Users\guosu\Desktop\Justins\python\chromedriver.exe')  #打开浏览器
upperlimit_gold = 425
lowerlimit_gold = 415
upperlimit_sliver = 6250
lowerlimit_sliver = 6150  

def batch_check_interest_rate(driver):

    # for home == r'F:\Python\Economic\chromedriver_win32\chromedriver.exe'
        
    elem = driver.get('https://finance.sina.com.cn/futures/quotes/AUTD.shtml')  #打开网址
    information = "" #把结果要保存的变量先初始化好
    open_price_final = "" #把开盘价先初始化好
    twenty_average = ""

    def get_info(day_date=0):   #定义主要方法： 复制利率到information变量，并同时打印出来 
        nonlocal information,open_price_final,twenty_average  #调用父方法的变量
        status = driver.page_source #把全网页源代码文本保存在status变量里 
        price = status[status.find('real-price price'):status.find('change-wrap')] #找到第二个/tr后的内容，就是说提取前两行信息的意思
        open_price = status[status.find('open-price'):status.find('max-price')]
        filtered_open_price = re.findall('">(.*?)</',open_price)
        filtered_price = re.findall('">(.*?)</',price)
        if day_date==1:
            driver.find_element_by_xpath('.//*[@data-id="KKE_tab_kd"]').click()
            time.sleep(3)
            status = driver.page_source #把刷新后的网页源代码文本保存在status变量里 
            twenty_average_try = status[status.find('MA20:'):status.find('MA30: ')]
            filtered_20_average = re.findall('>(.*?)</',twenty_average_try)
            twenty_average = twenty_average_try[6:11] #这里有个问题，如果位数不同可能会出错
        #print('The latest rate is: \n')
        information = filtered_price[0]
        open_price_final = filtered_open_price[0]
        #print('\n')  #前后空好行 

    # 正式开始执行查询近期rate任务
    #print('for US:\n')

    get_info()
    print("目前金价为："+information)  #根据需要打印出利率详情统计。
    #print("Open price is "+open_price_final)

    def popup(a=1):
        if a==1:
            root = tk.Tk()
            root.wm_attributes('-topmost',1)
            root.title("价格提醒")
            root.geometry('500x300')
            label = tk.Label(root, text='Gold price is:\n'+str(information))
            label.pack()
            root.mainloop()
        if a==2:
            root = tk.Tk()
            root.wm_attributes('-topmost',1)
            root.title("可加仓")
            root.geometry('500x300')
            label = tk.Label(root, text='可加仓, price difference is:\n'+str(float(open_price_final)-float(information)))
            label.pack()
            root.mainloop()
        if a==3:
            root = tk.Tk()
            root.wm_attributes('-topmost',1)
            root.title("减仓")
            root.geometry('500x300')
            label = tk.Label(root, text='可减仓，price difference is:\n'+str(float(information)-float(open_price_final)))
            label.pack()
            root.mainloop()
        if a==4:
            root = tk.Tk()
            root.wm_attributes('-topmost',1)
            root.title("小心，出现了极端情况！！！")
            root.geometry('500x300')
            label = tk.Label(root, text='小心，出现了极端情况！！！， price difference is:\n'+str(float(information)-float(open_price_final)))
            label.pack()
            root.mainloop()
                    
    if float(information) > upperlimit_gold or float(information) < lowerlimit_gold:
        popup()


    #check out AG trending
    global upperlimit_sliver,lowerlimit_sliver  #在下面需要inupt修改的话就需要在这里定义好是global的
    elem = driver.get('https://finance.sina.com.cn/futures/quotes/AGTD.shtml')
    information = "" #把结果要保存的变量先初始化好
    get_info()
    print("AGTD price is "+information)  #根据需要打印出利率详情统计。
    #print("AG Open price is "+open_price_final)
    
    if float(information) > upperlimit_sliver or float(information) < lowerlimit_sliver:
        popup()
        if float(information) > upperlimit_sliver:
            upperlimit_sliver=float(input("请输入银价提示新上限"))
        if float(information) < lowerlimit_sliver:
            lowerlimit_sliver=float(input("请输入银价提示新下限"))
        

    '''
    #check out CME gold trending    
    elem = driver.get('https://finance.sina.com.cn/futures/quotes/GC.shtml')
    information = "" #把结果要保存的变量先初始化好
    get_info(1)
    print(information)  #根据需要打印出利率详情统计。
    print("CME Open price is "+open_price_final)
    print("20 day average is "+twenty_average+". \n And the difference between 20 day and now is "+str(abs(float(twenty_average)-float(information)))+"(Normally is should be within 48 and 95% in 76) ")
    '''


    #check out CME Silver trending    
    elem = driver.get('https://finance.sina.com.cn/futures/quotes/SI.shtml')
    information = "" #把结果要保存的变量先初始化好
    get_info(1)
    print("CME Silver price is "+information)  #根据需要打印出利率详情统计。
    #print("CME Open price is "+open_price_final)
    print("info & 20 are :"+str(twenty_average)+str(information))
    print("20 day average is "+twenty_average+". \n And the difference between 20 day and now is "+str(abs(float(twenty_average)-float(information)))+"(Normally is should be within 1.4 and 95% in 2.38) ")

    '''
    # comparing open with close
    if (float(information)-float(open_price_final)) > 0.47:
        popup(3)
    if (float(open_price_final)-float(information)) > 0.47:
        popup(2)
    if abs(float(information)-float(open_price_final)) > 0.93:
        print("已超过95的可能性")
        popup(4)
    '''

    # comparing open with close
    if (float(information)-float(twenty_average)) > 1.4:
        popup(3)
    if (float(twenty_average)-float(information)) > 1.4:
        popup(2)
    if abs(float(information)-float(twenty_average)) > 2.38:
        print("已超过95的可能性")
        popup(4)

    # compare with average, 68%<49, 99%<108
    if abs(float(information)-float(open_price_final)) > 1.4:
        popup(4)
    if abs(float(information)-float(twenty_average)) > 3.38:
        popup(4)


    
while True:
    try:
        batch_check_interest_rate(driver)
    except Exception as a:
        print(a)
    time.sleep(300)
